from django.db import models
from utils.models import CoreModel

# Create your models here.

STATUS_CHOICES = (
    (0, "禁用"),
    (1, "启用"),
)


class Users(CoreModel):
    username = models.CharField(max_length=25, unique=True, db_index=True, verbose_name='用户账号',
                                help_text="用户账号")
    password = models.CharField(max_length=255, help_text="用户密码", verbose_name="用户密码")
    email = models.EmailField(max_length=255, verbose_name="邮箱", null=True, blank=True, help_text="邮箱")
    mobile = models.CharField(max_length=255, verbose_name="电话", null=True, blank=True, help_text="电话")
    avatar = models.TextField(verbose_name="头像", null=True, blank=True, help_text="头像")
    name = models.CharField(max_length=40, verbose_name="姓名", help_text="姓名")
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name="状态", help_text="状态")
    GENDER_CHOICES = (
        (1, "男"),
        (2, "女"),
    )
    gender = models.IntegerField(choices=GENDER_CHOICES, default=1, verbose_name="性别", null=True, blank=True,
                                 help_text="性别")
    SUPER_CHOICES = (
        (1, "超级管理员"),
        (2, "普通管理员"),
        (3, "系统运维"),
    )
    is_superuser = models.IntegerField(choices=SUPER_CHOICES, default=2, verbose_name="用户类型", null=True, blank=True,
                                       help_text="用户类型")

    class Meta:
        db_table = "system_users"
        verbose_name = '用户表'
        verbose_name_plural = verbose_name


class UserRole(models.Model):
    user_id = models.IntegerField(default=0, verbose_name="用户ID", help_text="用户ID")
    role_id = models.IntegerField(default=0, verbose_name="角色ID", help_text="角色ID")

    class Meta:
        db_table = 'system_users_role'
        verbose_name = '用户角色关系表'
        verbose_name_plural = verbose_name


class UserPost(models.Model):
    user_id = models.IntegerField(default=0, verbose_name="用户ID", help_text="用户ID")
    post_id = models.IntegerField(default=0, verbose_name="岗位ID", help_text="岗位ID")

    class Meta:
        db_table = 'system_users_post'
        verbose_name = '用户岗位关系表'
        verbose_name_plural = verbose_name


class Post(CoreModel):
    name = models.CharField(null=False, max_length=64, verbose_name="岗位名称", help_text="岗位名称")
    code = models.CharField(max_length=32, verbose_name="岗位编码", help_text="岗位编码")
    STATUS_CHOICES = (
        (0, "离职"),
        (1, "在职"),
    )
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name="岗位状态", help_text="岗位状态")

    class Meta:
        db_table = "system_post"
        verbose_name = '岗位表'
        verbose_name_plural = verbose_name


class Dept(CoreModel):
    name = models.CharField(max_length=64, verbose_name="部门名称", help_text="部门名称")
    owner = models.CharField(max_length=32, verbose_name="负责人", null=True, blank=True, help_text="负责人")
    phone = models.CharField(max_length=32, verbose_name="联系电话", null=True, blank=True, help_text="联系电话")
    email = models.EmailField(max_length=32, verbose_name="邮箱", null=True, blank=True, help_text="邮箱")
    status = models.BooleanField(default=True, verbose_name="部门状态", null=True, blank=True,
                                 help_text="部门状态")
    parent = models.ForeignKey(to='Dept', on_delete=models.PROTECT, default=None, verbose_name="上级部门",
                               db_constraint=False, null=True, blank=True, help_text="上级部门")

    class Meta:
        db_table = "system_dept"
        verbose_name = '部门表'
        verbose_name_plural = verbose_name


class Role(CoreModel):
    name = models.CharField(max_length=64, verbose_name="角色名称", help_text="角色名称")
    code = models.CharField(max_length=64, unique=True, verbose_name="角色编码", help_text="角色编码")
    status = models.IntegerField(choices=STATUS_CHOICES, default=1, verbose_name="状态", help_text="状态")

    class Meta:
        db_table = 'system_role'
        verbose_name = '角色表'
        verbose_name_plural = verbose_name


class RoleMenu(models.Model):
    role_id = models.IntegerField(default=0, verbose_name="角色ID", help_text="角色ID")
    menu_id = models.IntegerField(default=0, verbose_name="菜单ID", help_text="菜单ID")

    class Meta:
        db_table = 'system_role_menu'
        verbose_name = '角色菜单关系表'
        verbose_name_plural = verbose_name


class RoleDept(models.Model):
    role_id = models.IntegerField(default=0, verbose_name="角色ID", help_text="角色ID")
    dept_id = models.IntegerField(default=0, verbose_name="部门ID", help_text="部门ID")

    class Meta:
        db_table = 'system_role_dept'
        verbose_name = '角色部门关系表'
        verbose_name_plural = verbose_name


class RolePermission(models.Model):
    role_id = models.IntegerField(default=0, verbose_name="角色ID", help_text="角色ID")
    permission = models.CharField(max_length=255, verbose_name="权限许可", help_text="权限许可")

    class Meta:
        db_table = 'system_role_permission'
        verbose_name = '角色权限关系表'
        verbose_name_plural = verbose_name


class Menu(CoreModel):
    icon = models.CharField(max_length=64, default="", verbose_name="菜单图标",
                            help_text="菜单图标")
    title = models.CharField(max_length=64, verbose_name="菜单名称", help_text="菜单名称")
    permission = models.CharField(max_length=64, null=True, blank=True, verbose_name="权限标识", help_text="权限标识")
    ISLINK_CHOICES = (
        (0, "否"),
        (1, "是"),
    )
    is_ext = models.BooleanField(default=False, verbose_name="是否外链", help_text="是否外链")
    TYPE_CHOICES = (
        (0, "目录"),
        (1, "菜单"),
    )
    type = models.IntegerField(choices=TYPE_CHOICES, verbose_name="是否目录", help_text="是否目录")
    path = models.CharField(max_length=128, verbose_name="路由地址", null=True, blank=True, help_text="路由地址")
    redirect = models.CharField(max_length=128, verbose_name="重定向地址", null=True, blank=True,
                                help_text="重定向地址")
    component = models.CharField(max_length=128, verbose_name="组件地址", null=True, blank=True, help_text="组件地址")
    name = models.CharField(max_length=50, verbose_name="组件名称", null=True, blank=True, help_text="组件名称")
    status = models.BooleanField(default=True, blank=True, verbose_name="菜单状态", help_text="菜单状态")
    parent = models.ForeignKey(to='Menu', on_delete=models.CASCADE, verbose_name="上级菜单", null=True, blank=True,
                               db_constraint=False, help_text="上级菜单")

    class Meta:
        db_table = "system_menu"
        verbose_name = '菜单表'
        verbose_name_plural = verbose_name


class OperationLog(CoreModel):
    request_username = models.CharField(max_length=50, blank=True, null=True, verbose_name="请求用户",
                                        help_text="请求用户")
    request_time = models.CharField(max_length=50, blank=True, null=True, verbose_name="请求耗时",
                                    help_text="请求耗时")
    request_modular = models.CharField(max_length=64, verbose_name="请求模块", null=True, blank=True,
                                       help_text="请求模块")
    request_path = models.CharField(max_length=400, verbose_name="请求地址", null=True, blank=True,
                                    help_text="请求地址")
    request_body = models.TextField(verbose_name="请求参数", null=True, blank=True, help_text="请求参数")
    request_method = models.CharField(max_length=8, verbose_name="请求方式", null=True, blank=True,
                                      help_text="请求方式")
    request_msg = models.TextField(verbose_name="操作说明", null=True, blank=True, help_text="操作说明")
    request_ip = models.CharField(max_length=32, verbose_name="请求ip地址", null=True, blank=True,
                                  help_text="请求ip地址")
    request_browser = models.CharField(max_length=64, verbose_name="请求浏览器", null=True, blank=True,
                                       help_text="请求浏览器")
    response_code = models.CharField(max_length=32, verbose_name="响应状态码", null=True, blank=True,
                                     help_text="响应状态码")
    request_os = models.CharField(max_length=64, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    json_result = models.TextField(verbose_name="返回信息", null=True, blank=True, help_text="返回信息")
    address = models.CharField(max_length=64, verbose_name="IP来源地址", null=True, blank=True, help_text="IP来源地址")
    status = models.BooleanField(default=False, verbose_name="响应状态", help_text="响应状态")

    class Meta:
        db_table = 'system_operation_log'
        verbose_name = '操作日志'
        verbose_name_plural = verbose_name


class ErrorLog(CoreModel):
    request_username = models.CharField(max_length=50, blank=True, null=True, verbose_name="请求用户",
                                        help_text="请求异常用户")
    request_time = models.CharField(max_length=50, blank=True, null=True, verbose_name="请求耗时",
                                    help_text="请求耗时")
    request_modular = models.CharField(max_length=64, verbose_name="请求模块", null=True, blank=True,
                                       help_text="请求模块")
    request_path = models.CharField(max_length=400, verbose_name="请求地址", null=True, blank=True,
                                    help_text="请求地址")
    request_body = models.TextField(verbose_name="请求参数", null=True, blank=True, help_text="请求参数")
    request_method = models.CharField(max_length=8, verbose_name="请求方式", null=True, blank=True,
                                      help_text="请求方式")
    request_msg = models.TextField(verbose_name="操作说明", null=True, blank=True, help_text="操作说明")
    request_ip = models.CharField(max_length=32, verbose_name="请求ip地址", null=True, blank=True,
                                  help_text="请求ip地址")
    request_browser = models.CharField(max_length=64, verbose_name="请求浏览器", null=True, blank=True,
                                       help_text="请求浏览器")
    response_code = models.CharField(max_length=32, verbose_name="响应状态码", null=True, blank=True,
                                     help_text="响应状态码")
    request_os = models.CharField(max_length=64, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    json_result = models.TextField(verbose_name="返回信息", null=True, blank=True, help_text="返回信息")
    result = models.TextField(verbose_name="返回中文信息", null=True, blank=True, help_text="返回中文信息")
    address = models.CharField(max_length=64, verbose_name="IP来源地址", null=True, blank=True, help_text="IP来源地址")
    status = models.BooleanField(default=False, verbose_name="响应状态", help_text="响应状态")

    class Meta:
        db_table = 'system_error_log'
        verbose_name = '异常日志'
        verbose_name_plural = verbose_name


class LoginLog(CoreModel):
    LOGIN_TYPE_CHOICES = (
        (1, '普通登录'),
    )
    username = models.CharField(max_length=32, verbose_name="登录用户名", null=True, blank=True, help_text="登录用户名")
    ip = models.CharField(max_length=32, verbose_name="登录ip", null=True, blank=True, help_text="登录ip")
    agent = models.TextField(verbose_name="agent信息", null=True, blank=True, help_text="agent信息")
    browser = models.CharField(max_length=200, verbose_name="浏览器名", null=True, blank=True, help_text="浏览器名")
    os = models.CharField(max_length=200, verbose_name="操作系统", null=True, blank=True, help_text="操作系统")
    continent = models.CharField(max_length=50, verbose_name="州", null=True, blank=True, help_text="州")
    country = models.CharField(max_length=50, verbose_name="国家", null=True, blank=True, help_text="国家")
    province = models.CharField(max_length=50, verbose_name="省份", null=True, blank=True, help_text="省份")
    city = models.CharField(max_length=50, verbose_name="城市", null=True, blank=True, help_text="城市")
    district = models.CharField(max_length=50, verbose_name="县区", null=True, blank=True, help_text="县区")
    isp = models.CharField(max_length=50, verbose_name="运营商", null=True, blank=True, help_text="运营商")
    area_code = models.CharField(max_length=50, verbose_name="区域代码", null=True, blank=True, help_text="区域代码")
    country_english = models.CharField(max_length=50, verbose_name="英文全称", null=True, blank=True,
                                       help_text="英文全称")
    country_code = models.CharField(max_length=50, verbose_name="简称", null=True, blank=True, help_text="简称")
    longitude = models.CharField(max_length=50, verbose_name="经度", null=True, blank=True, help_text="经度")
    latitude = models.CharField(max_length=50, verbose_name="纬度", null=True, blank=True, help_text="纬度")
    login_type = models.IntegerField(default=1, choices=LOGIN_TYPE_CHOICES, verbose_name="登录类型",
                                     help_text="登录类型")

    class Meta:
        db_table = 'system_login_log'
        verbose_name = '登录日志'
        verbose_name_plural = verbose_name
        ordering = ('-create_datetime',)
